<!--
 * @Author: MrLee
 * @Date: 2024-08-24 08:55:14
 * @LastEditors: MrLee
 * @LastEditTime: 2024-09-02 21:33:55
 * @Description: 请填写简介
-->
<template>
  <custom-navbar title="新增教育经历" :auto-back="true" left-icon-color="#303133" />
  <up-gap height="30rpx" />
  <view class="px-24rpx">
    <infocard :user-info-list="moreList" @selectSchool="getSchoolDetails" @selectYuanXiao="getProject" />
  </view>
  <view class="flex fixed bottom-0 h-178rpx w-[100%] bg-#fff shadow-[0px_0px_12px_0px_rgba(238,239,241,0.50)]">
    <view @click="deleteItem"
      class="mx-auto my-0 mt-[24rpx] h-82rpx w-[40%] rounded-[42rpx] bg-#f7f7f8 text-center text-[30rpx] text-[#1d2129] font-medium leading-[82rpx]">
      删除
    </view>
    <view
      class="mx-auto my-0 mt-[24rpx] h-82rpx w-[40%] rounded-[42rpx] bg-#1f50f2 text-center text-[30rpx] text-white font-medium leading-[82rpx]"
      @click="onsubmit()">
      添加
    </view>
  </view>
</template>

<script setup lang="ts">
  import infocard from '@/pages/components/infocard/index.vue';
  import { projectList, schoolDetail, schoolList } from "@/api/common";
  import { addEdu, delEdu, getEduDetails, getEduUpdate } from '@/api/user';

  const moreList = reactive([
    {
      title: '学历',
      value: '请选择',
      label: '请选择',
      valueType: 'select',
    },
    {
      title: '学校名称',
      value: '请选择',
      label: '请选择',
      search: true,
      valueType: 'select',
    },
    {
      title: '学院',
      value: '请选择',
      label: '请选择',
      search: true,
      valueType: 'select',
    },
    {
      title: '专业',
      value: '请选择',
      label: '请选择',
      search: true,
      valueType: 'select',
    },
    {
      title: '时间',
      value: '请选择',
      valueType: 'dateRang',
    },
    {
      title: '留学经历',
      value: '请选择',
      valueType: 'switch',
    },
  ]);


  const onsubmit = async () => {
    console.log('提交了', moreList)
    if (!moreList) {
      uni.showToast({
        title: '请返回重新加载页面',
        icon: 'none'
      })
      return
    }
    if (moreList[0].label == "请选择") {
      uni.showToast({
        title: '请填写学历',
        icon: 'none'
      })
      return
    }
    if (moreList[1].label == "请选择") {
      uni.showToast({
        title: '请选择学校',
        icon: 'none'
      })
      return
    }
    if (moreList[2].label == "请选择") {
      uni.showToast({
        title: '请选择学院',
        icon: 'none'
      })
      return
    }
    if (moreList[3].label == "请选择") {
      uni.showToast({
        title: '请选择专业',
        icon: 'none'
      })
      return
    }
    if (moreList[4].value == "请选择") {
      uni.showToast({
        title: '请选择时间',
        icon: 'none'
      })
      return
    }
    let [startTime, endTime] = moreList[4].value.split('-')
    const startDate = startTime.split('.').slice(0, 2).join('.');
    const endDate = endTime.split('.').slice(0, 2).join('.');
    let time = `${startDate}-${endDate}`;
    let params = {
      id: (itemId == "undefined" || itemId == "" || !itemId || itemId == undefined) ? '' : itemId,
      qualification: moreList[0].label,
      school_id: moreList[1].value,
      school_name: moreList[1].label,
      college_name: moreList[2].label,
      majors_name: moreList[3].label,
      time: time,
      status_overseas_study: !!moreList[4].value ? 1 : 2
    }
    console.log('params', params)
    // return
    if (itemId) {
      await getEduUpdate(params)
    } else {
      await addEdu(params)
    }
    // uni.redirectTo({
    //   url: '/pages/tab/home/more/educationExperience'
    // })
    uni.navigateBack()
  }

  const getSchoolList = () => {
    schoolList().then(res => {
      if (res) {
        moreList[1].selectOptions = res.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        })
      }
    })
  }

  const getSchoolDetails = (id) => {
    console.log(id, '选择完院校了')
    schoolDetail(id).then(res => {
      if (res) {
        moreList[2].selectOptions = res.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        })
      }
    })
  }
  const getProject = (id) => {
    console.log(id, '选择完学院了')
    projectList(id).then(res => {
      if (res) {
        moreList[3].selectOptions = res.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        })
      }
    })
  }

  const deleteItem = () => {
    uni.showModal({
      title: '提示',
      content: '确定删除吗？',
      success: async (res) => {
        if (res.confirm) {
          await delEdu({ id: itemId })
          // uni.redirectTo({
          //   url: '/pages/tab/home/more/educationExperience'
          // })
          uni.navigateBack()
        }
      }
    })
  }
  const getDetails = () => {
    getEduDetails({ id: itemId }).then(res => {
      if (res) {
        moreList[0].value = res.qualification
        moreList[0].label = res.qualification
        moreList[1].value = res.school_id
        moreList[1].label = res.school_name
        moreList[2].value = res.college_id
        moreList[2].label = res.college_name
        moreList[3].value = res.majors_id
        moreList[3].label = res.majors_name
        moreList[4].value = res.time
        moreList[4].label = res.time
        moreList[5].value = res.status_overseas_study
        moreList[5].label = res.status_overseas_study == 1 ? '是' : '否'
      }
    })
  }

  let itemId = reactive('')
  onLoad((params) => {
    itemId = params.id
    if (itemId) {
      getDetails()
    }
  })
  onMounted(() => {
    getSchoolList();
    let value = JSON.parse(uni.getStorageSync('config')).configCli
    moreList[0].selectOptions = value.config_qualification$$学历配置.map(item => {
      return {
        label: item.value,
        value: item.value
      }
    })
  })
</script>
